<?php
namespace App\Controller;

class OrderController extends BaseController
{
    public function getOrderList()
    {
        $order = D("Order")->getOrderList(array("user_id" => session("userId")), true, "id desc");
        $this->assign("order", $order);
        $this->display();
    }

    public function getOrder()
    {
        $order = D("Order")->getOrder(I("get."), true);
        $this->assign("order", $order);
        $this->display();
    }

    public function getOrderJson()
    {
        $order = D("Order")->getOrder(I("get."), true);
        $this->ajaxReturn($order);
    }

    public function getOrderListJson()
    {
        $order = D("Order")->getOrderList(I("get."), true, "id desc");
        $this->ajaxReturn($order);
    }

    public function updateOrderDetailJson()
    {
        $data = I("get.");
        unset($data["id"]);
        D("OrderDetail")->updateOrderDetail(I("get.id"), $data);
    }

    public function updateUserMoney($userId, $money)
    {
        $user = D("User")->getUser(array("id" => $userId));
        $balance = floatval($user["money"]) + floatval($money);
        if ($balance >= 0) {
            D("User")->updateUser($userId, array("money" => $balance));
            return true;
        } else {
            return false;
        }
    }

    public function checkStore($cartdata)
    {
        $product = D("Product");
        foreach ($cartdata as $key => $value) {
            $result = $product->getProduct(array("id" => $value["id"]));
            if ($result["status"] == 0 || $result["status"] == -1) {
                return false;
            }

            if ($result["store"] > 0) {
                if (floatval($result["store"]) < floatval($value["num"])) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * @method post
     * name
     * phone
     * province
     * city
     * address
     * remark
     * deliveryTime
     * cartData
     * totalPrice
     * payment
     * code
     *
     * @return order
     */
    public function addOrderJson()
    {
        if (!$this->checkStore(I("post.cartData"))) {
            return false;
        }

        $payFlag = false;
        if (I("post.payment") == "0") {
            $payFlag = $this->updateUserMoney(session("userId"), -I("post.totalPrice"));
        }

        if (I("post.contact_id")) {
            $contact_id = I("post.contact_id");
        } else {
            $contact = array();
            $contact ["name"] = I("post.name");
            $contact ["city"] = I("post.city");
            $contact ["province"] = I("post.province");
            $contact ["phone"] = I("post.phone");
            $contact ["address"] = I("post.address");
            $contact ["district"] = I("post.district");
            $contact ["postcode"] = I("post.postcode");
            $contact ["user_id"] = session("userId");
            $contact_id = D("Contact")->addContact($contact);
        }

        $discount = 0;
        $data = I("post.");
        $order_id = D("Order")->addOrder(session("userId"), $contact_id, $payFlag, $discount, $data);
        $order = D("Order")->getOrder(array("id" => $order_id), true);

        request_by_fsockopen($this->appUrl . U("Admin/Wechat/sendTplMsgOrder"), array("openid" => session("userOpenId"), "order_id" => $order_id));

        if (I("post.payment") == "1") {
            $wxConfig = D("WxConfig")->getWxSet();
            if ($wxConfig["switch"] == 0) {
                $order["payUrl"] = U("Pay/wxPay", array("id" => $order_id));
            } else {
                $order["payUrl"] = U("Pay/wxQrcodePay", array("id" => $order_id));
                $order["payUrlMent"] = 1;
            }
        } elseif (I("post.payment") == "2") {
            $order["payUrl"] = U("Pay/alipay", array("id" => $order_id));
        }

        $this->ajaxReturn($order);
    }

    public function cancelOrderJson()
    {
        D("Order")->cancelOrder(I("get.id"));
    }

    public function updateOrderJson()
    {
        D("Order")->updateOrder(I("get."));
    }

    public function commentOrderJson()
    {
        $orderDetail = D("Order")->getOrderDetail(I("get.id"));

        $addAll = array();
        foreach ($orderDetail as $key => $value) {
            $data = array();
            $data ["product_id"] = $value["product_id"];
            $data ["user_id"] = session("userId");
            $data ["name"] = I("get.name");
            array_push($addAll, $data);
        }

        D("Comment")->addComment($addAll);
    }
}